var chiTitle = [
    {field: "ck", checkbox: true},
    {field: "pro1", title: "定点医疗机构编号", hide: true},
    {field: "pro2", title: "个人编号"},
    {field: "pro3", title: "门诊(住院)流水号", sort: true},
    {field: "pro4", title: "单据号", sort: true},
    {field: "pro5", title: "交易类型", sort: true},
    {field: "pro29", title: "结算时间", sort: true},
    {field: "pro6", title: "医院交易流水号", sort: true},
    {field: "pro7", title: "中心交易流水号", sort: true},
    {field: "pro8", title: "医疗费总额", sort: true},
    {field: "pro9", title: "本次帐户支付"},
    {field: "pro10", title: "统筹支付金额"},
    {field: "pro11", title: "大病救助基金支付", hide: true},
    {field: "pro12", title: "公务员补助支付", hide: true},
    {field: "pro13", title: "离休统筹支付", hide: true},
    {field: "pro14", title: "1-6级伤残军人统筹支付", hide: true},
    {field: "pro15", title: "医院负担金额", hide: true},
    {field: "pro16", title: "本次现金支付"},
    {field: "pro17", title: "自费费用", hide: true},
    {field: "pro18", title: "乙类自理费用", hide: true},
    {field: "pro19", title: "特检自理费用", hide: true},
    {field: "pro20", title: "特治自理费用", hide: true},
    {field: "pro21", title: "起付标准自付", hide: true},
    {field: "pro22", title: "进入统筹费用", hide: true},
    {field: "pro23", title: "统筹分段自付", hide: true},
    {field: "pro24", title: "进入救助金费用", hide: true},
    {field: "pro25", title: "救助金自付", hide: true},
    {field: "pro26", title: "超封顶线自付", hide: true},
    {field: "pro27", title: "符合基本医疗费用", hide: true},
    {field: "pro28", title: "住院次数", hide: true},
    {title: '操作', templet: '#barDemo', fixed: "right", align: "center"}
];

var sjmTitle = [{field: "ck", checkbox: true},
    {field: "pro1", title: "就诊编号", sort: true},
    {field: "pro2", title: "个人编号", sort: true},
    {field: "pro3", title: "结算编号", sort: true},
    {field: "pro4", title: "支付类别"},
    {field: "pro27", title: "结算时间", hide: true},
    {field: "pro5", title: "参保所属机构", hide: true},
    {field: "pro6", title: "清算类别", hide: true},
    {field: "pro7", title: "清算分中心", hide: true},
    {field: "pro8", title: "医疗费总额", sort: true},
    {field: "pro9", title: "全自费金额", hide: true},
    {field: "pro10", title: "超限价自付金额", hide: true},
    {field: "pro11", title: "先行自付金额", hide: true},
    {field: "pro12", title: "符合范围部分", hide: true},
    {field: "pro13", title: "本次起付线"},
    {field: "pro14", title: "实际支付起付线", hide: true},
    {field: "pro15", title: "基本医疗基金支付", hide: true},
    {field: "pro16", title: "基本医疗比例自付", hide: true},
    {field: "pro17", title: "大病保险基金支出", hide: true},
    {field: "pro18", title: "民政救助支付"},
    {field: "pro19", title: "精准扶贫补充医疗支付", hide: true},
    {field: "pro20", title: "个人账户支出", hide: true},
    {field: "pro21", title: "精准扶贫提高金额（基本医疗）", hide: true},
    {field: "pro22", title: "五免费用总额", hide: true},
    {field: "pro23", title: "医院负担部分"},
    {field: "pro24", title: "个人现金支出"},
    {field: "pro25", title: "基金支付金额总计", hide: true},
    {field: "pro26", title: "本次个人账户支付后帐户余额", hide: true},
    {field: "pro28", title: "医疗人员类别", hide: true},
    {field: "pro29", title: "执行社会保险办法", hide: true},
    {title: '操作', minWidth: 50, templet: '#barDemo', fixed: "right", align: "center"}
];

layui.use(['form', 'table', 'layer', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        laydate = layui.laydate;


    laydate.render({
        elem: '#startDate'
        , theme: '#393D49',
        calendar: true,
        min: '2019-09-27',
        value: new Date()
    });


    laydate.render({
        elem: '#endDate'
        , theme: '#393D49',
        calendar: true,
        min: '2019-09-27',
        value: new Date()
    });

    //监听退费
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定退费 个人编号为:' + data.pro2 + ' 总费用为:' + data.pro8 + ' 的交易么？', {
                title: "提示",
                icon: 3,
                skin: 'layer-ext-moon'
            }, function (index) {
                layer.msg('调用医保接口中...', {
                    icon: 16
                    , shade: 0.08
                });
                if (pulginObj.call(data)) {
                    layer.msg('退费成功...', {time: 3000, icon: 6});
                    obj.del();
                    layer.close(index);
                }
                ;
            });
        }
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        //var result = JSON.stringify(data.field);
        var index = layer.msg('调用医保接口中...', {
            icon: 16
            , shade: 0.08
        });
        var str = pulginObj.query(data.field);
        layer.close(index);
        //执行搜索重载
        if (str == undefined || str == '') {
            //layer.alert("医保返回数据为空,未查询到交易信息...");
        } else {
            index = layer.msg('拼命加载中...', {
                icon: 16
                , shade: 0.08
            });
            var columns;
            var yblx = $("#yblx").val();
            if (yblx == '42030002') {
                columns = sjmTitle;
            } else {
                columns = chiTitle;
            }
            $.ajax({
                url: "/hwn/queryUnilateral",
                type: "POST",
                data: {
                    startDate: data.field.startDate,
                    endDate: data.field.endDate,
                    yblx: data.field.yblx,
                    str: str
                },
                success: function (result) {
                    console.info(result);
                    layer.close(index);
                    if (result.code == '0') {
                        if (result.data.length > 0) {
                            table.render({
                                elem: '#test',
                                //'/hwn/queryUnilateral'
                                data: result.data,
                                toolbar: true, //开启头部工具栏，并为其绑定左侧模板,
                                defaultToolbar: ['filter', 'exports', 'print'],
                                cols: [columns],
                                page: true,
                                cellMinWidth: 80,
                                height: 'full-180',
                                limits: [20, 50, 100, 200],
                                limit: 20
                            })
                        } else {
                            layer.msg('当前条件下,无单边数据...', {time: 3000, icon: 1});
                        }
                    } else {
                        layer.alert(result.msg, {
                            title: "系统异常",
                            icon: 2
                        });
                    }

                    // table.reload('test',{
                    //     data:result.data
                    // })
                },
                error: function (xhr, msg) {
                    layer.alert(xhr.code + " " + msg);
                }
            });
        }
        ;
        return false;
    });

    table.render({
        elem: '#test',
        //'/hwn/queryUnilateral'
        data: [],
        toolbar: true, //开启头部工具栏，并为其绑定左侧模板,
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: [chiTitle],
        page: true,
        cellMinWidth: 80,
        height: 'full-180',
        limits: [20, 50, 100, 200],
        limit: 20
    });

    pulginObj = {
        pulgin: function () {
            return document.getElementById('embed');
        },
        call: function (row) {
            var flag = false;
            var yblx = $("#yblx").val();
            var jysr = "";
            if (row) {
                if (yblx == '42030002') {
                    jysr += "<?xml version='1.0' encoding='GBK' standalone='yes' ?>";
                    jysr += "<input>";
                    jysr += "<akc190>" + row.pro1 + "</akc190>";
                    jysr += "<aaz216>" + row.pro3 + "</aaz216>";
                    jysr += "<aac001>" + row.pro2 + "</aac001>";
                    jysr += "<aka130>" + row.pro4 + "</aka130>";
                    jysr += "<ykb065>" + row.pro29 + "</ykb065>";
                    jysr += "<aae011>" + "单边处理" + "</aae011>";
                    jysr += "<ykc141>" + "单边处理" + "</ykc141>";
                    jysr += "<aae013>" + "单边处理" + "</aae013>";
                    jysr += "</input>";
                } else {
                    var timestamp = new Date().getTime();
                    jysr = "2430^" + row.pro1 + "^10882^^" + formatDate(timestamp) + "^0000^" + row.pro3 + "|" + row.pro4 + "|" + row.pro29 + "|0|||系统管理员|0|||^";
                }
                var str = '{"jybh":"42","jysr":"' + jysr + '","jxflag":' + ',"InsuranceCode":"' + yblx + '","loadCard":true}';
                console.info(str);
                var result = pulginObj.pulgin().onExecute(str);
                var jsonObj = JSON.parse(result);
                console.info(result);
                if (!jsonObj.flag) {
                    layer.alert('退费失败,' + jsonObj.msg);
                } else {
                    flag = true;
                }
            } else {
                layer.alert('请先选择数据！！！');
            }
            return flag;
        },
        query: function (data) {

            //sleep(1000);
            var yblx = data.yblx;
            var startDate = data.startDate;
            var endDate = data.endDate;
            var timestamp = new Date().getTime();
            var qybm = "";
            var jysr = "";
            var jybh = "";
            var bxbf = data.bxbf;
            if (yblx == "42030002") {
                jybh = '46';
                jysr += "<?xml version='1.0' encoding='GBK' standalone='yes' ?>";
                jysr += "<input>";
                jysr += "<aae030>" + startDate + "</aae030>";
                jysr += "<aae031>" + endDate + "</aae031>";
                jysr += "<ykb065>" + bxbf + "</ykb065>";
                jysr += "<outputfile>D:\\log\\jsxx_" + bxbf + "_" + startDate + "_" + endDate + ".txt</outputfile>";
                jysr += "</input>";
            } else {
                if (yblx == "42030001") {
                    qybm = "12103";
                }
                if (yblx == "42030011") {
                    qybm = "42038101004";
                }
                jysr = "1100^" + qybm + "^10882^^" + formatDate(timestamp) + "^0000^" + startDate.replace("-", "").replace("-", "") + "|" + endDate.replace("-", "").replace("-", "") + "|^";
            }
            var jysc = $("#jysc");
            var jxflag = true;
            //var jysrstr = jysr.textbox('getValue');
            var str = "";
            try {
                if (yblx) {
                    str = '{"jybh":' + jybh + ',"jysr":"' + jysr + '","jxflag":' + jxflag + ',"InsuranceCode":"' + yblx + '"}';
                } else {
                    layer.alert("交易输入或交易编号不能为空...");
                    return;
                }
                console.info(str);
                // progress();
                var result = pulginObj.pulgin().onExecute(str);
                //sleep(10000);
                // disLoad();
                //替换文本中的制表符，换行符
                result = result.replace(/\t/g, "\\t").replace(/\r\n/g, "\\r\\n");
                console.info(result);
                var jsonObj = JSON.parse(result);
                if (!jsonObj.flag) {
                    layer.alert("医保返回交易失败," + jsonObj.msg);
                } else {
                    var strs = new Array();
                    strs = jsonObj.resultString.split(/\r\n/);
                    var str = '{"total":' + (strs.length - 1).toString() + ',"rows":[';
                    for (var i = 0; i < strs.length - 1; i++) {
                        var strs1 = new Array();
                        strs1 = strs[i].split(/\t/);
                        str += "{";
                        for (var j = 0; j < strs1.length; j++) {
                            if (j != strs1.length - 1) {
                                str += '"pro' + (j + 1).toString() + '":"' + strs1[j] + '",';
                            } else {
                                str += '"pro' + (j + 1).toString() + '":"' + strs1[j] + '"';
                            }
                        }
                        if (i != strs.length - 2) {
                            str += "},";
                        } else {
                            str += "}";
                        }
                    }
                    str += "]}";
                    console.info(str);
                    return str;

                    // $('#tt').datagrid({data:JSON.parse(str)});
                    //doSearch(jsonObj.resultString);
                }
                //jysc.textbox('setValue', result);
            } catch (e) {
                layer.alert('系统异常,' + e.message);
            } finally {

            }
        }
    };

//时间格式转换
    function formatDate(inputTime) {
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return "" + y + m + d + h + minute + second;
    }

});









